/**
 * 网络请求方法
 * 
 * @param params
 *            参数
 * @param url
 *            请求地址
 * @param successCallBack
 *            成功回调
 */
function ajaxUtils(params, url, successCallBack) {
	$.ajax({
		type : "POST",
		data : params,
		url : url,
		async : true,
		dataType : "json",
		success : function(result) {
			successCallBack(strToJson(result));
		},
		failure : function(result) {
			showError(result);
		},
	})
}
/**
 * 获取当前日期
 * 
 * @returns {String}
 */
function getNowFormatDate() {
	var myDate = new Date();
	var seperator1 = "-";
	var seperator2 = ":";
	var year = myDate.getFullYear();
	var month = myDate.getMonth() + 1;
	var strDate = myDate.getDate();
	if (month >= 1 && month <= 9) {
		month = "0" + month;
	}
	if (strDate >= 0 && strDate <= 9) {
		strDate = "0" + strDate;
	}
	var currentdate = year + seperator1 + month + seperator1 + strDate + " "
			+ myDate.getHours() + seperator2 + myDate.getMinutes() + seperator2
			+ myDate.getSeconds();
	return currentdate;
}
/**
 * 清除div中的内容
 * 
 * @param divId
 */
function clearDiv(divId) {
	$("#" + divId).empty();
}
/**
 * 获取某个父容器下的所有选中的checkbox的值
 * 
 * @param divId
 * @param checkboxName
 * @returns {String}
 */
function getCheckBox(parentId, checkboxName) {
	var arrChk = $("#" + parentId).find(
			"input[name='" + checkboxName + "']:checked");
	var str = "";
	$(arrChk).each(function() {
		str = str + this.value + ",";
	});
	var size = str.length - 1;
	str = str.substring(0, size);
	return str;
}
function ajaxUtilsNoAsync(params, url, successCallBack) {
	$.ajax({
		type : "POST",
		data : params,
		url : url,
		async : false,
		dataType : "json",
		success : function(result) {
			successCallBack(strToJson(result));
		},
		failure : function(result) {
			showError(result);
		},
	})
}
function strToJson(obj) {
	var isjson = typeof (obj) == "object"
			&& Object.prototype.toString.call(obj).toLowerCase() == "[object object]"
			&& !obj.length;
	if (isjson) {
		return obj;
	} else {
		return jQuery.parseJSON(obj);
	}
}
/**
 * 跳转页面
 * 
 * @param pagePath
 */
function openPage(pagePath) {
	window.location.href = pagePath;
}
/**
 * 返回上一页并刷新
 */
function backAndRefresh() {
	self.location = document.referrer;
}
/**
 * 刷新当前页面
 */
function refresh() {
	location.reload();
}
/**
 * 返回上一页
 */
function goBack() {
	javascript: history.go(-1);
}
/**
 * json对象转换为table表格
 * 
 * @param jsonResult
 * @param tableId
 * @param otherParams
 * @returns {String}
 */
function jsonToTable(jsonResult, tableId, otherParams) {
	var string = "<table id='"
			+ tableId
			+ "' "
			+ otherParams
			+ " class='table table-bordered' data-resizable-columns-id='demo-table' border='1px'cellspacing='0' cellpadding='0'>";
	var length = jsonResult.length;
	if (length > 0) {
		string = string + "<tr>";
		jQuery.each(jsonResult[0], function(key, val) {
			string = string + "<th style='text-align: center;'>" + key
					+ "</th>";
		});
		string = string + "</tr>";
	}
	for (var i = 0; i < length; i++) {
		string = string + "<tr>";
		jQuery.each(jsonResult[i], function(key, val) {
			if (val == 'null') {
				val = "";
			}
			string = string + "<td style='text-align: center;'><div>" + val
					+ "</div></td>";
		});
		string = string + "</tr>";
	}
	string = string + "</table>";
	return string;
}
var nowShowdialogId;
var nowLeftMargin;
var nowTopMargin;
function showDialog(dialogId) {
	nowShowdialogId = dialogId;
	$(document.body).css({
		"overflow-x" : "hidden",
		"overflow-y" : "hidden"
	});
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var dialogBg = $("body #windowsDialogBg");
	if (dialogBg == null || dialogBg.length == 0) {
		$("body").append(
			"<div id=\"windowsDialogBg\" style=\"position: absolute;left:0px;top:0px;background-color:rgba(0, 0, 0, 0.3);width:"
					+ windowWidth + "px;height:" + windowHeight + "px\"></div>");
	}
	var dialogView = $("#" + dialogId);
	$("#windowsDialogBg").append(dialogView);
	// 先清除之前dialog的边距信息，防止设置新边距的时候出现偏差
	$("#windowsDialogBg #" + dialogId).css({
		"position":"absolute",
		"display" : "block",
		"left" : 0,
		"top" : 0
	});
	var dialogWidth = $("#windowsDialogBg #" + dialogId).outerWidth(true);
	var dialogHeight = $("#windowsDialogBg #" + dialogId).outerHeight(true);
	var leftMargin = (windowWidth - dialogWidth) / 2;
	nowLeftMargin = leftMargin;
	var leftTop = (windowHeight - dialogHeight) / 2;
	nowTopMargin = leftTop;
	var scrollTop = $(document).scrollTop();
	$("#windowsDialogBg #" + dialogId).css({
		"position":"absolute",
		"display" : "block",
		"left" : leftMargin,
		"top" : leftTop
	});
	$("#windowsDialogBg #" + dialogId).animate({
		"display" : "block",
		"position":"absolute",
		"left" : leftMargin,
		"top" : leftTop
	});
}
function cancelDialog() {
	$("#windowsDialogBg").children().css({
		"display" : "none"
	});
	$("body").append($("#windowsDialogBg").children());
	$("#windowsDialogBg").remove();
	$(document.body).css({
		"overflow-x" : "auto",
		"overflow-y" : "auto"
	});
//	$("#windowsDialogBg #" + nowShowdialogId).animate({
//		"display" : "block",
//		"position":"absolute",
//		"left" : nowLeftMargin,
//		"top" : nowTopMargin
//	}, function animateEnd() {
//		$("#windowsDialogBg").children().css({
//			"display" : "none"
//		});
//		$("body").append($("#windowsDialogBg").children());
//		$("#windowsDialogBg").remove();
//		$(document.body).css({
//			"overflow-x" : "auto",
//			"overflow-y" : "auto"
//		});
//	});
}
/**
 * 弹出正确的toast
 * 
 * @param msg
 *            提示文字
 * @returns 返回当前提示的div对象
 */
function showSuccess(msg) {
	var toastView = showToast(msg);
	toastView.css({
		"background-color" : "rgba(0, 255, 0, 1)",
		"color" : "rgba(255, 255, 255, 1)",
	});
	return toastView;
}
/**
 * 弹出错误的toast
 * 
 * @param msg
 *            提示文字
 * @returns 返回当前提示的div对象
 */
function showError(msg) {
	var toastView = showToast(msg);
	toastView.css({
		"background-color" : "rgba(255, 0, 0, 1)",
		"color" : "rgba(255, 255, 255, 1)",
	});
	return toastView;
}
function showToast(msg) {
	var windowWidth = $(window).width();
	var windowHeight = $(window).height();
	var dialogBg = $("body #toastView");
	if (dialogBg == null || dialogBg.length == 0) {
		$("body").append(
				"<div id=\"toastView\" style=\"position: absolute;width:"
						+ windowWidth + "px;height:auto\">" + msg + "</div>");
	} else {
		$("#toastView").text(msg);
	}
	$("#toastView").css({
		"display" : "block",
		"margin" : "0px",
		"left" : "0px",
		"bottom" : "0px",
		"line-height" : "30px",
		"text-align" : "center"

	});
	setTimeout("toastHide()", 2000);
	return $("#toastView");
}
/**
 * 隐藏提示框
 */
function toastHide() {
	$("#toastView").remove();
}
